package net.seabookchen.controller;

import java.util.List;
import java.util.logging.Logger;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.seabookchen.dao.NewsDao;
import net.seabookchen.pojo.News;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.google.appengine.api.datastore.EntityNotFoundException;


@Controller
public class NewsController extends CommonController<News> {

	private static final Logger log = Logger.getLogger(NewsController.class.getName());
	private NewsDao nDao;
	
	public NewsController() {
		if (nDao == null) nDao = new NewsDao(News.class);
	}
	
	@RequestMapping(value ="/news", method=RequestMethod.POST)
	public ModelAndView postNews(HttpServletRequest request, HttpServletResponse resp, Model model) {
		String newsStr = request.getParameter("news");
		News news = new News(newsStr);
		nDao.put(news);
		populateModel(model, news, RequestMethod.POST + "-news-" + news.getId());
		log.info("Post news: " + news);
		return new ModelAndView("showNews");
	}
	
	@RequestMapping(value ="/allnews", method=RequestMethod.GET)
	public ModelAndView listNews(HttpServletRequest request, HttpServletResponse resp, Model model) {
		List<News> allNews = nDao.listNews();
		model.addAttribute("action", "List All News");
		model.addAttribute("entities", allNews);
		log.info("List All News size: " + allNews.size());
		return new ModelAndView("showNews");
	}
	
	@RequestMapping(value ="/allnews/{index}", method=RequestMethod.GET)
	public ModelAndView listNews(HttpServletRequest request, HttpServletResponse resp, @PathVariable String index, Model model) {
		List<News> allNews = nDao.listNews(Integer.valueOf(index));
		//model.addAttribute("action", "List All News");
		model.addAttribute("entities", allNews);
		log.info("List All News size: " + allNews.size());
		return new ModelAndView("showNews");
	}
	
	@RequestMapping(value ="/news/{id}", method=RequestMethod.GET)
	public ModelAndView getNews(HttpServletRequest request, HttpServletResponse resp, @PathVariable String id, Model model) {
		News news = null;
		try {
			news = nDao.get(Long.parseLong(id));
		} catch (NumberFormatException e) {
			log.severe(e.getMessage());
		} catch (EntityNotFoundException e) {
			log.severe(e.getMessage());
		}
		populateModel(model, news, RequestMethod.GET + "-News-" +id);
		log.info("GET News: " + news);
		return new ModelAndView("showNews");
	}
	
	@RequestMapping(value ="/news/{id}", method=RequestMethod.PUT)
	public ModelAndView putNews(HttpServletRequest request, HttpServletResponse resp, @PathVariable String id, Model model) {
		String newsStr = request.getParameter("news");
		News news = null;
		try {
			news = nDao.get(Long.parseLong(id));
			news.setNews(newsStr);
			nDao.put(news);
		} catch (NumberFormatException e) {
			log.severe(e.getMessage());
		} catch (EntityNotFoundException e) {
			log.severe(e.getMessage());
		}
		populateModel(model, news, RequestMethod.GET + "-News-" +id);
		log.info("Put News: " + news);
		return new ModelAndView("showNews");
	}
	
	@RequestMapping(value ="/news/{id}", method=RequestMethod.DELETE)
	public ModelAndView deleteNews(HttpServletRequest request, HttpServletResponse resp, @PathVariable String id, Model model) {
		nDao.delete(Long.parseLong(id));
		model.addAttribute("action", RequestMethod.DELETE + "-News-" +id);
		log.info("Delete News: " + id);
		return new ModelAndView("showNews");
	}

}
